package thread;

/**
 * 线程礼让，就是说执行此方法的线程先暂停，让其他线程先执行，因此如果优先级高还是会自己有限执行
 * @author apple
 *
 */
public class YieldTest {

	public static void main(String[] args) {
		int count = 100;
		for(int i = 0; i < count; i++){
			Producer p = new Producer();
			p.setPriority(Thread.MAX_PRIORITY);
			p.start();
		}
		for(int i = 0; i < 1; i++){
			Consumer c = new Consumer();
			c.setPriority(Thread.MIN_PRIORITY);
			c.start();
		}

		System.out.println("主线程。。。");
	}
}

/**
 * 生产者线程
 */
class Producer extends Thread{ 
	public void run(){
		while(true){
			System.out.println("producer");
			yield();
		}
	}
}

/**
 * 消费者线程
 */
class Consumer extends Thread{ 
	public void run(){
		while(true){
			System.out.println("consumer");
		}
	}
}